package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "dish", key = "#dishDTO.categoryId")
    public Result saveDish(@RequestBody DishDTO dishDTO){
        dishService.addDishWithFlavors(dishDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> getPage(DishPageQueryDTO dto){
        return Result.success(dishService.getPage(dto));
    }

    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.updateDishWithFlavor(dishDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取菜品")
    public Result<DishVO> getDishById(@PathVariable Long id){
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    @DeleteMapping
    @ApiOperation("批量删除菜品")
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public Result batchDelete(String ids){
        dishService.batchDeleteDish(ids);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据菜品类别查询菜品")
    public Result<List<Dish>> getDishByCategory(Long categoryId){
        return Result.success(dishService.getDishByCategory(categoryId));
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用或禁用菜品")
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public Result startOrStopDish(@PathVariable int status,Long id){
        dishService.startOrStop(status,id);
        return Result.success();
    }




}
